Read in SF trees data:

#Read in the data:
sf_trees <-
  read.csv(here("data", "sf_trees", "sf_trees.csv"))

Basic Wrangling Reminders:

Refresh some skills for data wrangling and summary statistics using ‘dplyr’ & make a graph.

top_five_status <- sf_trees %>%  
  count(legal_status) %>% #count function - replaces need for group_by by recognizing groups in the data, counts values (replaces summarize n), and puts in a table - count by legal status 
  drop_na(legal_status) %>% #removes any rows with missing or NA values for variable you specify - think about missing values before you remove them (206 final lecture)
  rename(tree_count = n) %>% # new name = old name  
  relocate(tree_count) %>% #moves tree count column to the front 
  slice_max(tree_count, n =5) #allows you to identify the rows with the highest value of the variable you specify and keep the top # of what you specify

Make a graph of those top 5 observations by legal status

ggplot(data = top_five_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count)) +
  geom_col() +
  labs(x = "Legal Status",
       y = "Tree Count") +
  coord_flip() + #flips the axis to fit in long title names 
  theme_minimal() 

# Default for ggplot is to plot in alphabetical order of category 

A few more data wrangling refresher examples

Only want to keep observations (rows) for Blackwood Acacia Trees

blackwood_acacia <- sf_trees %>% 
  filter(str_detect(species, "Blackwood Acacia")) %>%  #look within a dataset and filter to obnly keep rows where within variable given the string is detected 
  select(legal_status, date, latitude, longitude) #picks columns you want to keep or exclude
 
## Make a faux map of the spatial orientation of this data using latitude and longitude:
ggplot(data = blackwood_acacia,
       aes(x = longitude,
            y = latitude)) +
  geom_point()
## Warning: Removed 27 rows containing missing values (geom_point).

Use ‘tidyr’ separate() and unite() functions

Useful for combining OR separating columns ie. in this data set there are scientific names separated by :: to the common name

sf_trees_sep <- sf_trees %>% 
  separate(species, into = c("spp_scientific", "spp_common"), sep = "::") #separates the species column into scientific name and common name separated at :: 

Example: tidyr:: unite() - combine 2 columns into 1 column

sf_treess_unite <- sf_trees %>% 
  unite("id_status", tree_id:legal_status, sep = "_cool_") #new column name, colon indicates from this column to the next, what is going to be the separator 

Make some actual maps of blackwood acacia trees in SF:

‘st_as_sf()’ to convert latitude and longitude to spatial coordinates.

#Convert latitude and longitude into spatial coordinates:
blackwood_acacia_sp <- blackwood_acacia %>% 
  drop_na(longitude,latitude) %>% #remove missing obs where lat and long are missing 
  st_as_sf(coords = c("longitude", "latitude"))

st_crs(blackwood_acacia_sp) = 4326 #set coordinate reference system 

ggplot(data = blackwood_acacia_sp) +
  geom_sf(color = "darkgreen")

Read in other data for the SF roads shapefile:

sf_map <- read_sf(here("data", "sf_map", "tl_2017_06075_roads.shp"))

# if there is an existing crs use st transform
st_transform(sf_map, 4326)
## Simple feature collection with 4087 features and 4 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -122.5136 ymin: 37.70813 xmax: -122.3496 ymax: 37.83213
## geographic CRS: WGS 84
## # A tibble: 4,087 x 5
##    LINEARID   FULLNAME     RTTYP MTFCC                                  geometry
##  * <chr>      <chr>        <chr> <chr>                          <LINESTRING [°]>
##  1 110498938… Hwy 101 S O… M     S1400 (-122.4041 37.74842, -122.404 37.7483, -…
##  2 110498937… Hwy 101 N o… M     S1400 (-122.4744 37.80691, -122.4746 37.80684,…
##  3 110366022… Ludlow Aly … M     S1780 (-122.4596 37.73853, -122.4596 37.73845,…
##  4 110608181… Mission Bay… M     S1400 (-122.3946 37.77082, -122.3929 37.77092,…
##  5 110366689… 25th Ave N   M     S1400 (-122.4858 37.78953, -122.4855 37.78935,…
##  6 110368970… Willard N    M     S1400 (-122.457 37.77817, -122.457 37.77812, -…
##  7 110368970… 25th Ave N   M     S1400 (-122.4858 37.78953, -122.4858 37.78952,…
##  8 110498933… Avenue N     M     S1400 (-122.3643 37.81947, -122.3638 37.82064,…
##  9 110368970… 25th Ave N   M     S1400  (-122.4854 37.78983, -122.4858 37.78953)
## 10 110367749… Mission Bay… M     S1400 (-122.3865 37.77086, -122.3878 37.77076,…
## # … with 4,077 more rows
ggplot(data= sf_map) +
  geom_sf()

Combine blackwood acacia tree observations & SF Roads Map:

ggplot() +
  geom_sf(data = sf_map,
          size = 0.1, #changes size of the lines
          color = "darkgrey") + 
  geom_sf(data = blackwood_acacia_sp, #plots spatial points for blackwood acacia trees on top of the road map 
          color = "red",
          size = 0.5) +
  theme_void()

Make this an interactive map: still interactive in the html file.

tmap_mode("view") #default is plot which is a static map --> sets to interactive viewing
## tmap mode set to interactive viewing
tm_shape(blackwood_acacia_sp) +
  tm_dots()